package com.lahuan.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lahuan.demo.entity.AccountUsd;
import com.lahuan.demo.entity.AccountUsd;
import com.lahuan.demo.entity.FrozeAccountUsd;
import com.lahuan.demo.mapper.AccountUsdMapper;
import com.lahuan.demo.service.AccountUsdService;
import com.lahuan.demo.service.FrozeAccountUsdService;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
* <p>
* usd账户服务实现类
* </p>
* @author lahuan
* @since 2022-01-02
*/
@DubboService(version = "1.0.0", interfaceClass = AccountUsdService.class, timeout = 5000)
public class AccountUsdServiceImpl extends ServiceImpl<AccountUsdMapper, AccountUsd> implements AccountUsdService {
    @Autowired
    AccountUsdService accountUsdService;
    @Autowired
    FrozeAccountUsdService frozeAccountUsdService;

    @Trace
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void froze(Long userId, BigDecimal money, String transNo) {
        String traceId = TraceContext.traceId();
        System.out.println(traceId);
        //冻结
        AccountUsd accountRmb = accountUsdService.getOne(new LambdaQueryWrapper<AccountUsd>().eq(AccountUsd::getUserId, userId));

        if ( accountRmb == null) {
            throw new RuntimeException("账户不存在");
        }
        if (accountRmb.getMoney().compareTo(money) < 0) {
            throw new RuntimeException("账户余额不足");
        }
        //减钱
        accountRmb.setMoney(accountRmb.getMoney().subtract(money));

        FrozeAccountUsd frozeRmb = new FrozeAccountUsd();
        frozeRmb.setFrozeMoney(money);
        frozeRmb.setUserId(userId);
        frozeRmb.setTransNo(transNo);

        accountUsdService.updateById(accountRmb);
        frozeAccountUsdService.save(frozeRmb);

    }
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void add(Long userId, BigDecimal money, String transNo) {
        AccountUsd accountRmb = accountUsdService.getOne(new LambdaQueryWrapper<AccountUsd>().eq(AccountUsd::getUserId, userId));
        if ( accountRmb == null) {
            throw new RuntimeException("账户不存在");
        }
        accountRmb.setMoney(accountRmb.getMoney().add(money));
        //加钱
        accountUsdService.updateById(accountRmb);
        //删除冻结表
        frozeAccountUsdService.remove(new LambdaQueryWrapper<FrozeAccountUsd>().eq(FrozeAccountUsd::getTransNo, transNo));

    }
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void rollback(Long userId, BigDecimal money, String transNo) {
        FrozeAccountUsd frozeAccountUsd = frozeAccountUsdService.getOne(new LambdaQueryWrapper<FrozeAccountUsd>().eq(FrozeAccountUsd::getTransNo, transNo));
        if (frozeAccountUsd != null) {
            //找到还钱的目标
            AccountUsd accountRmbTarget = accountUsdService.getOne(new LambdaQueryWrapper<AccountUsd>().eq(AccountUsd::getUserId, userId));
            //还钱
            accountRmbTarget.setMoney(accountRmbTarget.getMoney().add(money));
            accountUsdService.updateById(accountRmbTarget);
            //删除冻结表
            frozeAccountUsdService.remove(new LambdaQueryWrapper<FrozeAccountUsd>().eq(FrozeAccountUsd::getTransNo, transNo));
        }

    }
}
